﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataModel;
using BasicDataManagement;

namespace SystemWindow
{
    public partial class PurchaseForm : Form
    {
        OpFromDB op;
        List<MaterialObject> matList;
        string sUserName;
        decimal sumPrice;
        List<Reimbursementinfo> reimInfoList;
        List<Reimbursementinfo> ViewReimList;

        public PurchaseForm(SystemUser user)
        {
            InitializeComponent();
            op = OpFromDB.GetInstance();
            sUserName = user.StaffName + '(' + user.StaffJobNum + ')';
            reimInfoList = new List<Reimbursementinfo>();
            matList = op.LoadMaterialObjects();
            foreach (MaterialObject m in matList)
            {
                materialcomboBox.Items.Add(m.ObjectName);
            }
            materialcomboBox.Text = matList[0].ObjectName;
            Unitlabel.Text = matList[0].Unit;
        }
        //添加
        private void AddRembursebutton_Click(object sender, EventArgs e)
        {
            if (materialcomboBox.Text == "")
            {
                MessageBox.Show("报销项目不能为空");
                return;
            }
            if (PricetextBox.Text == "")
            {
                MessageBox.Show("报销单价不能为空");
                return;
            }
            if (NumbertextBox.Text == "")
            {
                MessageBox.Show("报销数量不能为空");
                return;
            }

            Reimbursementinfo reimInfo = new Reimbursementinfo();
            reimInfo.Project = materialcomboBox.Text;
            reimInfo.Price = Convert.ToDecimal(PricetextBox.Text);
            reimInfo.Number = Convert.ToInt32(NumbertextBox.Text);
            reimInfo.JobNumber = sUserName;
            reimInfo.Date = DateTime.Now;
            reimInfo.Sum = reimInfo.Price * reimInfo.Number;
            reimInfo.State = 0;//0-申请报销，1-已审批

            reimInfoList.Add(reimInfo);

            sumPrice += (decimal)(Convert.ToDouble(PricetextBox.Text) * Convert.ToDouble(NumbertextBox.Text));
            SumtextBox.Text = sumPrice.ToString();
            PricetextBox.Text = "";
            NumbertextBox.Text = "";
            RequestReimburselistView.Items.Add(new ListViewItem(new string[] { reimInfo.JobNumber, reimInfo.Date.ToString(), reimInfo.Project, reimInfo.Price.ToString(), reimInfo.Number.ToString(), reimInfo.Sum.ToString(), reimInfo.Reason, (reimInfo.State == 1) ? "已审批" : "申请报销" }));
        }

        private void materialcomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            Unitlabel.Text = matList[materialcomboBox.SelectedIndex].Unit;
        }

        private void Deletebutton_Click(object sender, EventArgs e)
        {
            
            if (RequestReimburselistView.SelectedIndices.Count == 0) {
                MessageBox.Show("请选择要删除的项！");
                return;
            }
            int index = RequestReimburselistView.SelectedIndices[0];
            RequestReimburselistView.Items.RemoveAt(index);
            sumPrice -= reimInfoList[index].Sum;
            SumtextBox.Text = sumPrice.ToString();
            reimInfoList.RemoveAt(index);
            RequestReimburselistView.Items.Clear();
            //刷新列表
            foreach (Reimbursementinfo reimInfo in reimInfoList)
            {
                RequestReimburselistView.Items.Add(new ListViewItem(new string[] { reimInfo.JobNumber, reimInfo.Date.ToString(), reimInfo.Project, reimInfo.Price.ToString(), reimInfo.Number.ToString(), reimInfo.Sum.ToString(), reimInfo.Reason, (reimInfo.State == 1) ? "已审批" : "申请报销" }));
            }
        }
        //申请报销
        private void Requestbutton_Click(object sender, EventArgs e)
        {
            if (reimInfoList.Count == 0)
            {
                MessageBox.Show("申请报销的清单为空！");
                return;
            }
            foreach (Reimbursementinfo i in reimInfoList)
            {
                op.AddReimbursement(i.JobNumber, i.Date, i.Project, i.Price, i.Number, i.Sum, "采购员报销", i.State);
            }
            MessageBox.Show("本次报销清单已提交，请等待审批！");
        }
        //今天报销记录
        private void ViewDayReimbutton_Click(object sender, EventArgs e)
        {
            ViewReimlistView.Items.Clear();
            List<Reimbursementinfo> TodayReimList = new List<Reimbursementinfo>();
            TodayReimList = op.LoadPurchaseRecord(TodaydateTimePicker.Value.Date, sUserName, false);
            ViewReimList = TodayReimList;
            if (TodayReimList.Count == 0)
            {
                MessageBox.Show("当天没有报销记录！");
                return;
            }
            foreach (Reimbursementinfo reimInfo in TodayReimList)
            {
                ViewReimlistView.Items.Add(new ListViewItem(new string[] { reimInfo.JobNumber, reimInfo.Date.ToString(), reimInfo.Project, reimInfo.Price.ToString(), reimInfo.Number.ToString(), reimInfo.Sum.ToString(), (reimInfo.State == 1) ? "已审批" : "申请报销" }));
            }
        }
        //所有报销记录
        private void ViewAllReimbutton_Click(object sender, EventArgs e)
        {
            ViewReimlistView.Items.Clear();
            List<Reimbursementinfo> AllReimList = new List<Reimbursementinfo>();
            AllReimList = op.LoadPurchaseRecord(TodaydateTimePicker.Value.Date, sUserName, true);
            ViewReimList = AllReimList;
            if (AllReimList.Count == 0)
            {
                MessageBox.Show("暂无报销记录！");
                return;
            }
            foreach (Reimbursementinfo reimInfo in AllReimList)
            {
                ViewReimlistView.Items.Add(new ListViewItem(new string[] { reimInfo.JobNumber, reimInfo.Date.ToString(), reimInfo.Project, reimInfo.Price.ToString(), reimInfo.Number.ToString(), reimInfo.Sum.ToString(), (reimInfo.State == 1) ? "已审批" : "申请报销" }));
            }
        }
    }
}
